<?php

/**
 * This is the model class for table "{{studying_process}}".
 *
 * The followings are the available columns in table '{{studying_process}}':
 * @property integer $id
 * @property integer $academy_id
 * @property integer $related_id
 * @property integer $language
 * @property string $academy_level
 * @property string $beginning_year
 * @property string $ending_year
 * @property string $school_name
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 * @property string $updated_fields
 *
 * The followings are the available model relations:
 * @property Academy $academy
 */
class StudyingProcess extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return StudyingProcess the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{studying_process}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('academy_id', 'required', 'message' => t ( 'MSG_REQUIRED', 'messages' )),
			array('academy_id', 'numerical', 'integerOnly'=>true, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
			array('beginning_year, ending_year', 'length', 'max'=>20, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('academy_level', 'length', 'max'=>10, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('school_name', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('created_by, updated_by', 'length', 'max'=>50, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, academy_id, academy_level, beginning_year, ending_year, school_name, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'academy' => array(self::BELONGS_TO, 'Academy', 'academy_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID', 'studyingProcess'),
			'academy_id' => t('LBL_ACADEMY', 'studyingProcess'),
			'academy_level' => t('LBL_ACADEMY_LEVEL', 'studyingProcess'),
			'beginning_year' => t('LBL_BEGINNING_YEAR', 'studyingProcess'),
			'ending_year' => t('LBL_ENDING_YEAR', 'studyingProcess'),
			'school_name' => t('LBL_SCHOOL_NAME', 'studyingProcess'),
			'create_date' => t('LBL_CREATE_DATE', 'studyingProcess'),
			'created_by' => t('LBL_CREATED_BY', 'studyingProcess'),
			'update_date' => t('LBL_UPDATE_DATE', 'studyingProcess'),
			'updated_by' => t('LBL_UPDATED_BY', 'studyingProcess'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('academy_id',$this->academy_id);
		$criteria->compare('academy_level',$this->academy_level,true);
		$criteria->compare('beginning_year',$this->beginning_year);
		$criteria->compare('ending_year',$this->ending_year);
		$criteria->compare('school_name',$this->school_name,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeSave() {
            if (parent::beforeSave()) {

                $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);

                //Get user is logging
                $logedInUser = User::model()->findByPk(Yii::app()->user->id);	   

                $this->update_date = $current_date;

                if (isset($logedInUser)) {
                    $this->updated_by = $logedInUser->username;
                }	                            

                $this->setIsNewRecord(false);

                return true;
            } else {
                return false;
            }
        }
}